/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Clientes.java
 *
 * Created on 05-mar-2012, 11:02:59
 */


import basedatos.GestorClientes;
import java.util.ArrayList;
import java.util.Collection;
import logica.cCliente;
import utilidades.UFactory;
import utilidades.UModeloTabla;

/**
 *
 * @author Alberto
 */
public class Clientes extends javax.swing.JFrame {

    GestorClientes gestor = (GestorClientes) UFactory.getInstancia("GClientes");
    Collection<cCliente> coll;

    /** Creates new form Clientes */
    public Clientes() {
        initComponents();
        coll = gestor.getTodos();
        this.jTableClientes.setModel(new UModeloTabla(getEncabezamientos(), getDatos(coll)));
        // TableModel m = new TableModel();

    }

    public ArrayList<String> getEncabezamientos() {
        ArrayList<String> coll = new ArrayList<String>();

        coll.add("COD");
        coll.add("NOMBRE");
        coll.add("DNI");
        coll.add("MOVIL");
        coll.add("ALTA");

        return coll;
    }

    public ArrayList<Object[]> getDatos(Collection<cCliente> coll) {

        ArrayList<Object[]> datos = new ArrayList<Object[]>();
        for (int i = 0; i < coll.size(); i++) {
            datos.add(new Object[5]);
            datos.get(i)[0] = ((cCliente) coll.toArray()[i]).getCodCliente().toString();
            datos.get(i)[1] = ((cCliente) coll.toArray()[i]).getNombre().toString();
            datos.get(i)[2] = ((cCliente) coll.toArray()[i]).getDni().toString();
            datos.get(i)[3] = ((cCliente) coll.toArray()[i]).getMovil().toString();
            datos.get(i)[4] = ((cCliente) coll.toArray()[i]).getFechaAlta().toString();
        }
        return datos;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelClientes = new javax.swing.JLabel();
        jButtonAñadir = new javax.swing.JButton();
        jLabelDni = new javax.swing.JLabel();
        jTextFieldDni = new javax.swing.JTextField();
        jButtonModificar = new javax.swing.JButton();
        jButtonBorrar = new javax.swing.JButton();
        jButtonFactura = new javax.swing.JButton();
        jButtonInforme = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableClientes = new javax.swing.JTable();
        jButtonBuscar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(Clientes.class);
        setTitle(resourceMap.getString("FormClientes.title")); // NOI18N
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 1024, 720));
        setMinimumSize(new java.awt.Dimension(1024, 720));
        setName("FormClientes"); // NOI18N

        jLabelClientes.setFont(resourceMap.getFont("jLabelClientes.font")); // NOI18N
        jLabelClientes.setText(resourceMap.getString("jLabelClientes.text")); // NOI18N
        jLabelClientes.setName("jLabelClientes"); // NOI18N

        jButtonAñadir.setText(resourceMap.getString("jButtonAñadir.text")); // NOI18N
        jButtonAñadir.setActionCommand(resourceMap.getString("jButtonAñadir.actionCommand")); // NOI18N
        jButtonAñadir.setName("jButtonAñadir"); // NOI18N
        jButtonAñadir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                AbrirAñadirCliente(evt);
            }
        });

        jLabelDni.setFont(resourceMap.getFont("jLabelDni.font")); // NOI18N
        jLabelDni.setText(resourceMap.getString("jLabelDni.text")); // NOI18N
        jLabelDni.setName("jLabelDni"); // NOI18N

        jTextFieldDni.setText(resourceMap.getString("jTextFieldDni.text")); // NOI18N
        jTextFieldDni.setName("jTextFieldDni"); // NOI18N

        jButtonModificar.setText(resourceMap.getString("jButtonModificar.text")); // NOI18N
        jButtonModificar.setActionCommand(resourceMap.getString("jButtonModificar.actionCommand")); // NOI18N
        jButtonModificar.setName("jButtonModificar"); // NOI18N

        jButtonBorrar.setText(resourceMap.getString("jButtonBorrar.text")); // NOI18N
        jButtonBorrar.setActionCommand(resourceMap.getString("jButtonBorrar.actionCommand")); // NOI18N
        jButtonBorrar.setName("jButtonBorrar"); // NOI18N

        jButtonFactura.setText(resourceMap.getString("jButtonFactura.text")); // NOI18N
        jButtonFactura.setActionCommand(resourceMap.getString("jButtonFactura.actionCommand")); // NOI18N
        jButtonFactura.setName("jButtonFactura"); // NOI18N

        jButtonInforme.setText(resourceMap.getString("jButtonInforme.text")); // NOI18N
        jButtonInforme.setActionCommand(resourceMap.getString("jButtonInforme.actionCommand")); // NOI18N
        jButtonInforme.setName("jButtonInforme"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTableClientes.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jTableClientes.setName("jTableClientes"); // NOI18N
        jScrollPane1.setViewportView(jTableClientes);
        jTableClientes.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTableClientes.columnModel.title0")); // NOI18N
        jTableClientes.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTableClientes.columnModel.title1")); // NOI18N
        jTableClientes.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTableClientes.columnModel.title2")); // NOI18N

        jButtonBuscar.setText(resourceMap.getString("jButtonBuscar.text")); // NOI18N
        jButtonBuscar.setName("jButtonBuscar"); // NOI18N
        jButtonBuscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BuscarPorDNI(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(292, 292, 292)
                        .addComponent(jLabelClientes))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(67, 67, 67)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelDni)
                                .addGap(110, 110, 110)
                                .addComponent(jTextFieldDni, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(78, 78, 78)
                                .addComponent(jButtonBuscar))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(74, 74, 74)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonAñadir)
                            .addComponent(jButtonModificar)
                            .addComponent(jButtonBorrar)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jButtonInforme)
                                .addComponent(jButtonFactura)))))
                .addContainerGap(340, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabelClientes)
                .addGap(57, 57, 57)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDni)
                    .addComponent(jTextFieldDni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBuscar))
                .addGap(65, 65, 65)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jButtonAñadir)
                        .addGap(32, 32, 32)
                        .addComponent(jButtonModificar)
                        .addGap(33, 33, 33)
                        .addComponent(jButtonBorrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
                        .addComponent(jButtonFactura)
                        .addGap(40, 40, 40)
                        .addComponent(jButtonInforme)
                        .addGap(182, 182, 182))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );

        jButtonAñadir.getAccessibleContext().setAccessibleName(resourceMap.getString("jButtonAñadir.AccessibleContext.accessibleName")); // NOI18N
        jLabelDni.getAccessibleContext().setAccessibleName(resourceMap.getString("jLabelDni.AccessibleContext.accessibleName")); // NOI18N
        jTextFieldDni.getAccessibleContext().setAccessibleName(resourceMap.getString("jTextFieldDni.AccessibleContext.accessibleName")); // NOI18N
        jButtonModificar.getAccessibleContext().setAccessibleName(resourceMap.getString("jButtonModificar.AccessibleContext.accessibleName")); // NOI18N
        jButtonBorrar.getAccessibleContext().setAccessibleName(resourceMap.getString("jButtonBorrar.AccessibleContext.accessibleName")); // NOI18N
        jButtonFactura.getAccessibleContext().setAccessibleName(resourceMap.getString("jButtonFactura.AccessibleContext.accessibleName")); // NOI18N
        jButtonInforme.getAccessibleContext().setAccessibleName(resourceMap.getString("jButtonInforme.AccessibleContext.accessibleName")); // NOI18N

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1024)/2, (screenSize.height-720)/2, 1024, 720);
    }// </editor-fold>//GEN-END:initComponents

    private void BuscarPorDNI(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BuscarPorDNI
        // TODO add your handling code here:
        Collection<cCliente> coll;
        coll = gestor.buscar(this.jTextFieldDni.getText());
        this.jTableClientes.setModel(new UModeloTabla(getEncabezamientos(),getDatos(coll)) );
    }//GEN-LAST:event_BuscarPorDNI

    private void AbrirAñadirCliente(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AbrirAñadirCliente
        // TODO add your handling code here:
//        AñadirCliente formAñadirClientes = new AñadirCliente(cliente);
//        formAñadirClientes.setVisible(true);
//        this.setVisible(false);                
    }//GEN-LAST:event_AbrirAñadirCliente

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Clientes().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAñadir;
    private javax.swing.JButton jButtonBorrar;
    private javax.swing.JButton jButtonBuscar;
    private javax.swing.JButton jButtonFactura;
    private javax.swing.JButton jButtonInforme;
    private javax.swing.JButton jButtonModificar;
    private javax.swing.JLabel jLabelClientes;
    private javax.swing.JLabel jLabelDni;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableClientes;
    private javax.swing.JTextField jTextFieldDni;
    // End of variables declaration//GEN-END:variables
}
